﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SMS.Entities.BaseClasses;
using SMS.Entities;
using SMS.Model;
using SMS.Common.Silverlight;

namespace SMS.ViewModel.SalesModule
{
    public class SaleDetailFormViewModel : BaseNotify
    {
        SalesModel model = null;

        public Action<SaleDetailFormViewModel> ClosedMethod;
        public Action<SaleDetail, string> SalePropertyChanged;

        public SaleDetailFormViewModel()
        {
            if (App.IsRunning)
            {
                SaleDetail = new SaleDetail();
                SaleDetail.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SaleDetail_PropertyChanged);

                model = new SalesModel();
                model.GenerateSaleDetailCompleted += new Action<Entities.SaleDetail>(model_GenerateSaleDetailCompleted);

                CloseCommand = new DelegateCommand(Close_Clicked, null);

            }
        }

        void model_GenerateSaleDetailCompleted(SaleDetail obj)
        {
            if (obj != null)
            {
                int prod_id = SaleDetail.Product_Stock_ID;
                SaleDetail = obj;
                SaleDetail.Product_Stock_ID = obj.ProductStockWise.Product_Stock_ID;
                SaleDetail.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(SaleDetail_PropertyChanged);
                if (SalePropertyChanged != null)
                    SalePropertyChanged(SaleDetail, string.Empty);
            }
        }

        void SaleDetail_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "Product_Stock_ID")
            {
                model.GenerateSaleDetail(App.LoggedinUserName, SaleDetail.Product_Stock_ID);
            }

            if (SalePropertyChanged != null)
                SalePropertyChanged(sender as SaleDetail, e.PropertyName);
        }

        private SaleDetail _SaleDetail;
        public SaleDetail SaleDetail
        {
            get { return _SaleDetail; }
            set
            {
                _SaleDetail = value;
                OnPropertyChanged("SaleDetail");
            }
        }

        public ICommand CloseCommand { get; private set; }

        public void Close_Clicked(object param)
        {
            this.SaleDetail.IsLive = false;
            if (ClosedMethod != null)
                ClosedMethod(this);
        }

        private int _No;

        public int No
        {
            get { return _No; }
            set { _No = value; OnPropertyChanged("No"); }
        }

    }
}
